<?php
class SubjectController extends Controller{

	public function index(){
		$this->getSmarty();
		$this->smarty->display("admin/subject/subject.tpl");
	}


	/**
	 * 生成专题静态文件
	 * Enter description here ...
	 */
	public function createAllSubject() { //
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		require_once COMMON . DS . 'Constant.class.php';
		$service = new CommonDataService( $this->getDB () );
		$subsql = "SELECT * from  subject ";
		$results = $service->getResults($subsql);
		foreach ($results as $rs){
				
			$rssql="select * from subject_items t where subjectid = '".$rs->id."' order by t.index ";
			$subItems = $service->getResults($rssql);
			
			foreach ($subItems as $item){
				$item->img = Constant::getAmazonPic($item->img,250);
			}
			
			$this->smarty->assign ( 'subject', $rs);
			$this->smarty->assign ( 'subjectItems', $subItems);
			$content = $this->smarty->fetch("user_center/subject.tpl");
	 		$file_name = PROJECT.DS."subjects"."/".$rs->id.".html";   
		 	$fp = fopen($file_name, "w");          
		 	fwrite($fp, $content);          
		 	fclose($fp);
		 	
		 	//更新专题的url
		 	$url = "subjects"."/".$rs->id.".html";  
			$upsql = "update  subject  set url='".$url."' where id ='".$rs->id."'";
	 		$service->exSql($upsql);
		}
	}
	public function saveOrModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$service = new CommonDataService( $this->getDB () );

		$id = $_POST ["id"];
		if (empty ( $id )) {
			$input_data = array (
					'id' =>  uniqid (),
					'name' => $_POST ["name"],
					'desc' => $_POST ["desc"],
					'img' =>  $_POST ["img"],
					'index' => $_POST ["index"],
					'create_time' => date('Y-m-d'),
					'template' => $_POST ["template"]
			);
			$data = $service->add("subject",$input_data );
		} else {

			$input_data = array (
					'name' => $_POST ["name"],
					'desc' => $_POST ["desc"],
					'img' =>  $_POST ["img"],
					'index' => $_POST ["index"],
					'create_time' => date('Y-m-d'),
					'template' => $_POST ["template"]
			);

			$input_condition = array (
					'id' => $_POST ["id"]
			);
			$data = $service->edit("subject",$input_data, $input_condition );
		}
		echo "1";
	}
	function deleteSubject() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$service = new CommonDataService( $this->getDB () );
		$id =" id = '".trim ($_POST ['id'])."'";
		$service->delete("subject",$id);
		$sql = "delete from subject_items where subjectid = '".trim ($_POST ['id'])."'";
		$service->exSql($sql);
		echo -1;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );

		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $service->countNum ("subject");
		$result ["total"] = $row_count;
		$resource_infos = $service->resourcePage ("subject" ,$offset, $rows);
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function pageSubjectItems(){
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );

		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		$result = array ();
		$condition = " subjectid = '".$_POST["id"]."'";
		$row_count = $service->countNum ("subject_items",$condition);
		$result ["total"] = $row_count;
		$resource_infos = $service->resourcePage ("subject_items" ,$offset, $rows,$condition);
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function deleteSubItem() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$service = new CommonDataService( $this->getDB () );
		$id =" id = '".trim ($_POST ['id'])."'";
		$service->delete("subject_items",$id);
		echo -1;
	}
	function saveOrModifySubItem(){
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$service = new CommonDataService( $this->getDB () );

		$id = $_POST ["id"];
		if (empty ( $id )) {
			$input_data = array (
					'id' => md5 ( uniqid () ),
					'subjectid' => $_POST ["subjectid"],
					'name' => $_POST ["name"],
					'desc' => $_POST ["desc"],
					'img' =>  $_POST ["img"],
					'index' => $_POST ["index"],
					'create_time' => date('Y-m-d'),
					'url' => $_POST ["url"],
					'oldprice' => $_POST ["oldprice"],
					'newprice' => $_POST ["newprice"],
					'saveprice' => $_POST ["oldprice"]- $_POST ["newprice"],
					'unit' => '$',
					'returnpoints' => $_POST ["returnpoints"]
			);
			$data = $service->add("subject_items",$input_data );
		} else {

			$input_data = array (
					'subjectid' => $_POST ["subjectid"],
					'name' => $_POST ["name"],
					'desc' => $_POST ["desc"],
					'img' =>  $_POST ["img"],
					'index' => $_POST ["index"],
					'create_time' => date('Y-m-d'),
					'url' => $_POST ["url"],
					'oldprice' => $_POST ["oldprice"],
					'newprice' => $_POST ["newprice"],
					'saveprice' => $_POST ["oldprice"]- $_POST ["newprice"],
					'unit' => '$',
					'returnpoints' => $_POST ["returnpoints"]
			);

			$input_condition = array (
					'id' => $_POST ["id"]
			);
			$data = $service->edit("subject_items",$input_data, $input_condition );
		}
		echo "1";
	}

}